package com.gexin.controller.api;

import com.gexin.common.AppGlobal;
import com.gexin.common.cache.CacheUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.product.ProductSku;
import com.gexin.model.product.Products;
import com.gexin.model.shop.ShopInfo;
import com.gexin.plugin.sqlinxml.SqlKit;
import com.jfinal.kit.JsonKit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Product {
	public static Product api = new Product();
	/*获取抽奖的商品*/
	public static final String cache_Lottery_Product = "cache:product:getLotteryProduct";
	/*缓存商品详情的   接口  */
	public static final String cache_Product_Detail = "cache:product:detail1:";
	/**
	 * 获取抽奖商品的JSON
	 * @return
	 */
	public String cache_getLotteryProduct() {
		String json = CacheUtils.get(cache_Lottery_Product);
		if (StringUtil.isBlank(json)) {
			List<Products> list = Products.dao.find(SqlKit.sql("group_product.getLotteryProduct"));
			json = JsonKit.toJson(list);
			//不是开发模式
			if(!AppGlobal.dev_Mode){
			  CacheUtils.set(cache_Lottery_Product,json,60*5);//缓存五分钟
			}
		}
		return json;
	}
	
	/**
	 * 从缓存里面读取商品商品详情数据
	 * @param product_id  商品ID
	 * @return
	 */
	public   Map<String, Object>  cache_getProductsByID(Integer  product_id){
		Map<String, Object>  map  = CacheUtils.get(cache_Product_Detail+product_id);
		if(map==null){
			map  =  new HashMap<String, Object>();
			Products product = Products.dao.getGroupProductById(product_id);
			if(product==null){
				return null;
			}
			map.put("product_name", product.getProductName());
			map.put("banner", product.getImageBanner().split(","));
			map.put("detail", product.getDetailImg());
			map.put("product", product);
			map.put("stock_text", ProductSku.dao.cache_getStockByProduct(product_id));
			map.put("shop", ShopInfo.dao.cache_getShopInfoById(product.getShopId()));
			if (product.getAttrType()!=5) {
				CacheUtils.set(cache_Product_Detail+product_id,map,3600);//缓存1个小时
			}
		}
		return map;
	}  
}
